/*
 * BasePoint2PointCommunication.java    0.0.1    27/dic/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.transparency.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.mentalsmash.tazio.commons.identifiers.NID;
import org.mentalsmash.tazio.transparency.commons.messages.TazioMessage;
import org.mentalsmash.tazio.transparency.interfaces.Deliverer;
import org.mentalsmash.tazio.transparency.interfaces.MessageFactory;
import org.mentalsmash.tazio.transparency.interfaces.Point2PointCommunication;
import org.mentalsmash.tazio.transparency.interfaces.Resolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.name.Named;

/**
 * This is the main implementation for the {@link Point2PointCommunication} interface.
 * <p>
 * This class has to mask the upper layers from implementation specific issues. using this 
 * implementation the NIO Communication subsystem will be used. When sending a message, a 
 * {@link Deliverer} object is created and its {@link Deliverer#doDeliver() doDeliver()} 
 * method is invoked.
 * </p>
 * 
 * @version 0.0.1	27/dic/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 */
public class BasePoint2PointCommunication implements Point2PointCommunication {

	private final static Logger log = LoggerFactory.getLogger(BasePoint2PointCommunication.class);
	
	private final MessageFactory messageFactory;
	
	private final Resolver resolver;
	
	private final int MAX_SENDING_REPETITIONS;
	
	@Inject
	public BasePoint2PointCommunication(
			MessageFactory messageFactory,
			Resolver resolver,
			@Named("max_sending_repetitions")int MAX_SENDING_REPETITIONS){
		
		this.messageFactory = messageFactory;
		this.resolver = resolver;
		
		this.MAX_SENDING_REPETITIONS = MAX_SENDING_REPETITIONS;
	}
	
	/* (non-Javadoc)
	 * @see org.mentalsmash.tazio.transparency.interfaces.Point2PointCommunication#send(java.io.Serializable, org.mentalsmash.tazio.commons.identifiers.TazioNodeID)
	 */
	@Override
	public <T extends Serializable> Serializable send(T message, NID nid) {
		
		Serializable answer = null;
		
		TazioMessage toDeliver = messageFactory.encapsulateMessage(message);
		
		List<NID> recipients = new ArrayList<NID>();
		recipients.add(nid);
		
		Deliverer deliverer = new BaseDeliverer(resolver, toDeliver, recipients, MAX_SENDING_REPETITIONS);
		
		answer = deliverer.doDeliver();
		
		return answer;
		
	}

}
